package com.sneakxy.actions;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.sneakxy.beetl.ActionTemplate;
import com.sneakxy.beetl.TemplateDir;
import com.sneakxy.config.Config;

/**
 * 创建项目
 * @author 潜行的虚影
 *
 */
public class ActionCreate implements Action {

	private ActionTemplate template;
	
	private Config config;
	
	private Map<String, Object> params = new LinkedHashMap<>();
	
	private Map<String, Object> formatter = new LinkedHashMap<>();
	
	@Override
	@SuppressWarnings("unchecked")
	public void execute(CommandLine command, Options options) throws Exception {
		this.template = createActionTemplate(command, options);
		this.config = createConfig(command, options);
		params.put("config", config);
		
		Map<String, Object> map = JSON.parseObject(JSON.toJSONString(config.getProject()), Map.class);
		formatter.putAll(map);
		
		TemplateDir baseDir = new TemplateDir("", config.getPath());
		baseDir.mkdirs();
		renderDirectory(this.template.getFile(), baseDir);
	}
	
	
	
	@Override
	public Map<String, Object> getWriteParams() {
		return params;
	}

	@Override
	public Map<String, Object> getNameFormatter() {
		return formatter;
	}

	@Override
	public ActionTemplate getActionTemplate() {
		return template;
	}

	public boolean isAllowed(String name) {
		return StringUtils.endsWith(name, "[p]") || !StringUtils.endsWith(name, "[c]"); 
	}
	
}
